package com.dongjiantong.app.controller.farmrecords.cowRecords;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.farmrecords.model.FarmRecordsHarmlesses;
import com.dongjiantong.farmrecords.service.FarmRecordsHarmlessesService;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * 牛档案-无害化记录
 * @author Lee
 * @date 2018/1/6
 */
@Slf4j
@OpenBean
public class FarmRecordsHarmlessesBullController {
    @Reference(version = "1.0.0")
    public FarmRecordsHarmlessesService farmRecordsHarmlessesService;

    /**
     * 根据关联牛档案id 查看免疫记录
     * @param cowRecordId
     * @return
     */
    @OpenMethod(key = "app.farmrcords.farmrecordsharmlesses.bull.selectBullImmunization",
            paramNames = "cowRecordId",
            httpMethods = RequestMethod.GET)
    public Response<FarmRecordsHarmlesses> selectBullHarmlessesNoCenter(Long cowRecordId) {
        if (cowRecordId == null) { throw new OPServerException("关联牛档案id不能为空"); }

        FarmRecordsHarmlesses farmRecordsHarmlesses = farmRecordsHarmlessesService.selectBullHarmlessesNoCenter(cowRecordId);

        return Response.ok(farmRecordsHarmlesses);
    }

    /**
     * 新增一条无害化记录 (暂存状态)
     * @param farmRecordsHarmlesses
     * @param toStatus 单据改变为状态
     * @return long 返回插入数据的id
     */
    @OpenMethod(key = "app.farmrecords.farmrecordsharmlesses.bull.createOne",
            paramNames = {"farmRecordsHarmlesses","toStatus"},
            httpMethods = RequestMethod.POST)
    public Response<Map<String,Object>> createOne(@RequestBody(required = false) FarmRecordsHarmlesses farmRecordsHarmlesses,
                                                  @RequestParam(required = false) Integer toStatus){
        Map<String,Object> result = null;

        if (farmRecordsHarmlesses == null) { throw new OPServerException(HttpCode.BAD_REQUEST,"请求参数对象不能为空"); }
        if (farmRecordsHarmlessesService == null){ throw new OPServerException("farmRecordsHarmlessesService 为null,请检查"); }

        LoginInfo loginInfo = UserUtil.getCurrentUser();

        result = farmRecordsHarmlessesService.createCowOne(loginInfo, farmRecordsHarmlesses, toStatus,2);

        if (result == null) {
            result = Maps.newHashMap();
            result.put("receiptId",null);
        }

        return Response.ok(result);
    }

    /**
     * 根据id或者number 更新一条单据信息
     * @param farmRecordsHarmlesses 请求的对象
     * @return  boolean (更新成功返回true 反之返回false)
     */
    @OpenMethod(key = "app.farmrecords.farmrecordsharmlesses.bull.updateByIdOrNumber",
            paramNames = {"farmRecordsHarmlesses","toStatus"},
            httpMethods = RequestMethod.POST)
    public Response<Boolean> updateByIdOrNumber(@RequestBody (required = false) FarmRecordsHarmlesses farmRecordsHarmlesses,
                                                @RequestParam(required = false) Integer toStatus){
        if (farmRecordsHarmlesses == null){
            throw new OPServerException(HttpCode.BAD_REQUEST,"请求参数对象不能为空");
        }else{
            if (farmRecordsHarmlessesService == null){ throw new OPServerException("farmRecordsHarmlessesService 为null,请检查"); }
            if (farmRecordsHarmlesses.getId() == null){ throw new OPServerException(HttpCode.PRIMARY_KEY_ID_IS_NOT_EXIST,HttpCode.PRIMARY_KEY_ID_IS_NOT_EXIST.getMessage()); }

            Integer con = farmRecordsHarmlessesService.updateByIdOrNumber(farmRecordsHarmlesses,toStatus);
            return Response.ok(con == 0 ? false : true);
        }
    }
}
